//
//  LoginViewController.m
//  WorkNest
//
//  Created by qbm360 on 15/7/6.
//  Copyright (c) 2015年 nest. All rights reserved.
//

#import "LoginViewController.h"
#import "CustomTabBarViewController.h"
#import "RegisterViewController.h"
#import "UserModel.h"
#import "ForgetPasswordViewController.h"

@interface LoginViewController () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *phoneNumberText;
@property (weak, nonatomic) IBOutlet UITextField *passwordText;
@property (weak, nonatomic) IBOutlet UIView *borderView;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UIButton *directLogin;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setLeftBarButtonItem:nil];
    self.title = NSLocalizedString(@"登录", nil);
//    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
    [UIUtil configTextField:_phoneNumberText withTitle:NSLocalizedString(@"帐号", nil)];
    [UIUtil configTextField:_passwordText withTitle:NSLocalizedString(@"密码", nil)];
    
    self.borderView.layer.borderWidth = 1;
    self.borderView.layer.borderColor = [[UIUtil colorWithHexString:@"#bdbaba"] CGColor];
    
    [_phoneNumberText addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"注册", nil) style:UIBarButtonItemStyleBordered target:self action:@selector(registButton)];
    self.directLogin.hidden = YES;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Action Methods
- (IBAction)loginButton:(id)sender {
    [self.view endEditing:YES];
    if ([_phoneNumberText.text isEqualToString:@""] || [_passwordText.text isEqualToString:@""]) {
        [[ESHUDView sharedInstance] showErrorMessage:@"用户名或密码不能为空" dismissBlock:nil];
        return;
    } else {
        _loginButton.enabled = NO;
        NSString *deviceId = @"";
        if ([[NSUserDefaults standardUserDefaults] valueForKey:@"KGeTuiDeviceToken"]) {
            deviceId = [[NSUserDefaults standardUserDefaults] valueForKey:@"KGeTuiDeviceToken"];
        }
        NSDictionary *loginParam = @{@"userName": _phoneNumberText.text, @"password": _passwordText.text, @"systemId":systemId, @"deviceId":deviceId,@"type":@"ios"};
        [RequestData loginParameters:[UIUtil toJsonString:loginParam] withBlock:^(UserModel *userData, NSString *msg, NSError *error) {
            if (error) {
                _loginButton.enabled = YES;
                [[ESHUDView sharedInstance] showErrorMessage:msg == nil?@"登录失败":msg dismissBlock:nil];
            } else {
                [[ESHUDView sharedInstance] showSuccessMessage:msg dismissBlock:nil];
                [[UserModel sharedInstance] setUserToken:userData.userToken];
                [[UserModel sharedInstance] setUserName:userData.userName];
                CustomTabBarViewController *tabBarView = [[CustomTabBarViewController alloc] init];
                self.view.window.rootViewController = tabBarView;
                [self.view.window addSubview: tabBarView.view];
                [self.view removeFromSuperview];
            }
        }];
    }
}

- (IBAction)goToHomeButton:(id)sender {
    CustomTabBarViewController *tabBarView = [[CustomTabBarViewController alloc] init];
    self.view.window.rootViewController = tabBarView;
    [self.view.window addSubview: tabBarView.view];
    [self.view removeFromSuperview];
}

- (IBAction)forgetPasswordButton:(id)sender {
    [self.navigationController pushViewController:[ForgetPasswordViewController new] animated:YES];
}

- (void)registButton {
    [self.navigationController pushViewController:[RegisterViewController new] animated:YES];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField == _phoneNumberText) {
        [_passwordText becomeFirstResponder];
        return YES;
    }
    if (textField == _passwordText) {
        [_passwordText resignFirstResponder];
        return YES;
    }
    return NO;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)textFieldDidChange:(UITextField *)textField
{
    if (textField == self.phoneNumberText) {
        if (textField.text.length > 11) {
            textField.text = [textField.text substringToIndex:11];
        }
    }
}

@end
